<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				instCacheKey:'mar-jd-biddingStart-inst-cahce',
				userCacheKey:'mar-jd-biddingStart-user-cahce'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','salaryInstId','salaryInstName'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			clearUser(){
				let _that = this
				_that.$refs.autoform.clear("title1","userId")
				if(_that.subEditorObj["title2"]){
					_that.subEditorObj["title2"].workList = []
				}
			},
			changeUserId(id){
				let _that = this
				_that.$refs.autoform.changeEditor("title1","userId",{
					link:'../../public/search/search',
					"linkParam":{
						other:JSON.stringify({
							instId:id?id:'',
							userId: _that.userId,
							businessFormCode:_that.busCode
						}),
						url:'biddingShow/getUsers',
						namesplit:'/',
						initload:true,
						namefield:'username/salaryPosName/salaryPosInstName',
						cachename:_that.userCacheKey
					}
				})
			},
			getWorkParam(){
				let _that = this
				return {
					salesInstId:_that.$refs.autoform.formdata.instId,
					salesUserId:_that.$refs.autoform.formdata.userId
				}
			},
			clearInst(){
				let _that = this
				_that.$refs.autoform.clear("title1","instId")
				_that.$refs.autoform.clear("title1","instUserId")
				_that.$refs.autoform.clear("title1","parentInstId")
				_that.changeUserId()
				if(_that.subEditorObj["title2"]){
					_that.subEditorObj["title2"].workList = []
				}
			},
			getPrimary(id){
				let _that = this
				_that.om.request(
					"tposition/getPrimary",
					{
						instId:id
					}
				).then(res=>{
					let formdata = _that.$refs.autoform.formdata
					formdata.instUserId = res.value.userId
					formdata.instUserName = res.value.userName
					formdata.parentInstId = res.value.parentInstId
					formdata.parentInstName = res.value.parentInstName
				})
			},
			initLoad(options) {
				let _that = this;
				_that.busCode = options.code
				var fileId = options.fileId
				let ajaxArr = []
				if(fileId){
					ajaxArr.push({
						url:'biddingStart/getValue',
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zinstId",{
						defaultValue:_that.salaryInstId,
						defaultName:_that.salaryInstName
						},null,mainData)
					changeEditor("title1","instId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								userId:_that.userId,
								businessFormCode:options.code
							}),
							url:'biddingShow/getInst',
							initload:true,
							namefield:'instName',
							cachename:_that.instCacheKey
						}
					},null,mainData)
					changeEditor("title1","userId",{
						link:'../../public/search/search',
						"linkParam":{
							other:JSON.stringify({
								instId:'',
								userId: _that.userId,
								businessFormCode:options.code
							}),
							url:'biddingShow/getUsers',
							namesplit:'/',
							initload:true,
							namefield:'username/salaryPosName/salaryPosInstName',
							cachename:_that.userCacheKey
						}
					},null,mainData)
					if(_that.subEditorObj['title2']){
						let attr = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData["title2"]
						if(hasEditor("title2","biddingId",configData)){
							let title = getEditorTitle("title2","biddingId",configData)
								attr.titleTemplate = title+":<%= biddingCode %>"
						}
						changeEditor("title2","dataStr",attr,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[0].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[0].value[index])
								_that.$set(_that.formdata,index,res[0].value[index])
							}
						}
						if(_that.subEditorObj["title2"]){
							let datalist = res[0].value.dataList
							_that.subEditorObj["title2"].workList = datalist
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(datalist))
						}
						if(_that.formdata.instId){
							changeEditor("title1","userId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										instId:_that.formdata.instId,
										userId: _that.userId,
										businessFormCode:options.code
									}),
									url:'biddingShow/getUsers',
									namesplit:'/',
									initload:true,
									namefield:'username/salaryPosName/salaryPosInstName',
									cachename:_that.userCacheKey
								}
							},null,mainData)
						}
						
					}
					_that.$nextTick(function(){
						
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				if(!_that.edit){
					_that.om.formSave('biddingStart/doAdd',_that.formdata,_that)
				}else{
					_that.om.formSave('biddingStart/doUpdate',_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.instCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							if(!formdata.instId || formdata.instId != d.id){
								formdata.instId = d.id
								formdata.instName = d.instName
								_that.changeUserId(d.id)
								_that.getPrimary(d.id)
								_that.$refs.autoform.clear("title1","userId")
								if(_that.subEditorObj["title2"]){
									_that.subEditorObj["title2"].workList = []
								}
							}
							uni.removeStorage({
								key:_that.instCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							if(_that.subEditorObj["title2"]){
								_that.subEditorObj["title2"].workList = []
							}
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       